package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.SpecialtyInfoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2017/10/22 12:33
 * Changed by :  Benjamin.
 * Changed time: 2017/10/22 12:33
 * Class description:个人技术专长列表接口
 */

public class SpecialtyInfoListAdapter extends SectionedRecyclerViewAdapter<SpecialtyInfoListAdapter.ViewTopHolder, SpecialtyInfoListAdapter.ViewContentHolder, RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater mInflater;
    List<SpecialtyInfoBean.RowsBean> specialList;
    private SparseBooleanArray mBooleanMap;
    SpecialClickListener clickListener;
    ArrayList<String> list;
    SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    public interface SpecialClickListener {
        void onSpecialClick(ArrayList<String> list);

        void onFiveCountClick();
    }

    public void setOnclickListener(SpecialClickListener clickListener) {
        this.clickListener = clickListener;
    }

    //设置选中状态
    public void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
        list.clear();
        for (int i = 0; i < mSelectedPositions.size(); i++) {
            int tempId = mSelectedPositions.keyAt(i);
            if (mSelectedPositions.get(tempId)) {
                list.add(String.valueOf(tempId));
            }
        }
        clickListener.onSpecialClick(list);
    }

    //判断是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    public SpecialtyInfoListAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        mBooleanMap = new SparseBooleanArray();
        list = new ArrayList<>();
    }

    public void refresh(List<SpecialtyInfoBean.RowsBean> specialList) {
        this.specialList = specialList;
        this.notifyDataSetChanged();
    }

    @Override
    protected int getSectionCount() {
        int TotalCount = specialList == null ? 0 : specialList.size();
        return TotalCount;
    }

    @Override
    protected int getItemCountForSection(int section) {
        int count = specialList.get(section).getTechnicalExpertiseList().size();
        return specialList.get(section).getTechnicalExpertiseList() == null ? 0 : count;
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected ViewTopHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new ViewTopHolder(LayoutInflater.from(context).inflate(R.layout.item_special_type, parent, false));
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected ViewContentHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new ViewContentHolder(mInflater.inflate(R.layout.item_special_info, parent, false));
    }

    @Override
    protected void onBindSectionHeaderViewHolder(final ViewTopHolder holder, final int section) {
        holder.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpen = mBooleanMap.get(section);
                String text = isOpen ? "展开" : "关闭";
                mBooleanMap.put(section, !isOpen);
                holder.type.setText(text);
                notifyDataSetChanged();
            }
        });
        holder.type.setText(specialList.get(section).getTechnologyName());
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {
    }

    @Override
    protected void onBindItemViewHolder(ViewContentHolder holder, final int section, final int position) {
        final SpecialtyInfoBean.RowsBean.TechnicalExpertiseListBean bean = specialList.get(section).getTechnicalExpertiseList().get(position);
        holder.content.setText(bean.getExpertiseName());
        if (isItemChecked(bean.getExpertiseId())) {
            holder.content.setBackgroundDrawable(context.getDrawable(R.drawable.shape_special_checked));
            holder.content.setTextColor(context.getColor(R.color.white));
        } else {
            holder.content.setBackgroundDrawable(context.getDrawable(R.drawable.shape_special_simple));
            holder.content.setTextColor(context.getColor(R.color.colorMain));
        }
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    if (isItemChecked(bean.getExpertiseId())) {
                        //判断当前选中如果已经为选中过的就移除，同时设置索引为false
                        setItemChecked(bean.getExpertiseId(), false);
                        mSelectedPositions.delete(bean.getExpertiseId());
                    } else {
                        //技术专长最多选择五个
                        if (mSelectedPositions.size() >= 5) {
                            clickListener.onFiveCountClick();
                        } else {
                            //如果为为没选中的添加进集合并且设置选中状态
                            setItemChecked(bean.getExpertiseId(), true);
                        }
                    }
                    notifyDataSetChanged();

                }
            }
        });
    }

    static class ViewTopHolder extends RecyclerView.ViewHolder {
        TextView type;

        public ViewTopHolder(View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.tv_special_type);
        }
    }

    static class ViewContentHolder extends RecyclerView.ViewHolder {
        TextView content;

        public ViewContentHolder(View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.tv_special_info);
        }
    }
}
